<template>
    <div class="app-container">
        <div class="head">
            <img src="@/assets/img/Layout/position.png" />
            <p class="see-block">其他入库单</p>
        </div>
        <div class="view-container">
            <el-form :model="queryParams" ref="queryForm" :inline="true">
                <el-form-item label="按起止时间" prop="time">
                    <el-date-picker
                        v-model="time"
                        type="daterange"
                        range-separator="至"
                        style="width: 230px;"
                        @change="dataTime()"
                    >
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="业务员" prop="updateBy">
                    <el-input
                        v-model="queryParams.updateBy"
                        placeholder="请输入业务员名称"
                        clearable
                        size="small"
                        @keyup.enter.native="handleQuery"
                    />
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" icon="el-icon-search" size="mini" class="list-search btn-success" color="#5ED4D4"
                        @click="handleQuery"
                    >搜索</el-button>
                    <el-button icon="el-icon-refresh" size="mini" class="list-search"
                        @click="resetQuery"
                    >重置</el-button>
                </el-form-item>
            </el-form>
            <el-row :gutter="10" class="mb8">
                <el-col :span="1.5">
                    <el-button
                        class="btn-success"
                        type="primary"
                        icon="el-icon-plus"
                        size="mini"
                        @click="add()"
                        v-hasPermi="['system:otherInboundOrder:add']"
                    >新建</el-button>
                </el-col>
                <el-col :span="21">
                    <el-button
                        class="btn-scanCodeDel"
                        type="primary"
                        icon="el-icon-download"
                        size="mini"
                        @click="btnExport()"
                    >导出</el-button>
                </el-col>
                <el-col :span="1">
                    <el-popover placement="bottom" title="筛选列" trigger="click" width="40">
                        <el-checkbox-group v-model="checkedColumns" size="mini">
                            <el-checkbox v-for="item in checkBoxGroup" :key="item" :label="item" :value="item"></el-checkbox>
                        </el-checkbox-group>
                        <div title="筛选列" class="filter-table-col" slot="reference">
                            <img src="@/assets/img/screening.png" width="25" />
                        </div>
                    </el-popover>
                </el-col>
            </el-row>
            <el-table v-loading="loading" :data="list" @row-click="handleView" @selection-change="handleSelectionChange" border>
                <el-table-column type="selection" width="35" align="center" />
                <el-table-column label="序号" type="index" align="center" width="50" />
                <template v-for="(item, index) in tableHeaderData">
                    <el-table-column
                        :label="item.label"
                        :prop="item.prop"
                        :align="item.align"
                        :sortable="item.sortable"
                        :show-overflow-tooltip="true"
                        :key="index"
                        :width="item.width"
                        v-if="colData[index].isTrue"
                    >
                        <template slot-scope="scope">
                            <template v-if="item.label == '业务类型'">
                                {{ scope.row.businessType == '0' ? '其他杂项入库' : scope.row.businessType == '1' ? '盘亏单' : '' }}
                            </template>
                            <template v-else-if="item.label == '物料名称'">
                                <div v-for="(item, index) in scope.row.detailList" :key="index">
                                    {{ item.productName }}
                                </div>
                            </template>
                            <template v-else-if="item.label == '数量'">
                                <div v-for="(item, index) in scope.row.detailList" :key="index">
                                    {{ item.endNum }}
                                </div>
                            </template>
                            <template v-else-if="item.label == '单位'">
                                <div v-for="(item, index) in scope.row.detailList" :key="index">
                                    {{ item.unit }}
                                </div>
                            </template>
                            <template v-else-if="item.label == '仓库'">
                                <div v-for="(item, index) in scope.row.detailList" :key="index">
                                    {{ item.warehouse }}
                                </div>
                            </template>
                            <template v-else-if="item.label == '仓位'">
                                <div v-for="(item, index) in scope.row.detailList" :key="index">
                                    {{ item.positionName }}
                                </div>
                            </template>
                            <template v-else-if="item.label == '状态'">
                                <div v-if="scope.row.status == 0" style="color:#13C2C2">待入库</div>
                                <div v-if="scope.row.status == 1" style="color:#52C41A">已入库</div>
                            </template>
                            <span v-else>{{ scope.row[scope.column.property] }}</span>
                        </template>
                    </el-table-column>
                </template>
            </el-table>
            <pagination
                v-show="total > 0"
                :total="total"
                :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize"
                @pagination="getList"
            />
        </div>
    </div>
</template>

<script>
import { otherStorageList } from "@/api/system/warehouse/otherStorage"
import { queryWarehouse } from "@/api/system/warehouse/warehouse"
import { exportOtherInboundOrder } from "@/api/system/export/export"

export default {
    name: "list",
    data() {
        return {
            loading: true,
            list: [],
            // 总条数
            total: 0,
            // 查询参数
            queryParams: {
                inOut: 1,
                pageNum: 1,
                pageSize: 10,
                updateBy: undefined,
                startTime: '',
                endTime: ''
            },
            ids: [],
            time: [],
            tableHeaderData: [
                { label: "单据编号", align: "center", prop: "code", width: "150" },
                { label: "业务日期", align: "center", prop: 'businessDate', width: "180" },
                { label: "业务类型", align: "center", prop: "businessType", width: "110"},
                { label: "联系人", align: "center", prop: "contact"},
                { label: "联系电话", align: "center", prop: "phone"},
                { label: "联系地址", align: "center", prop: "address"},
                { label: "物料名称", align: "center", prop: "productName", width:"150" },
                { label: "数量", align: "center", prop: "endNum", width: "80" },
                { label: "单位", align: "center", prop: "unit", width: "80" },
                { label: "仓库", align: "center", prop: "warehouseName", width: "100" },
                { label: "仓位", align: "center", prop: "positionName", width: "100" },
                { label: "操作人", align: "center", prop: "updateBy", width: "100" },
                { label: "状态", align: "center", prop: "status" },
            ],
            colData: [
                { title: "单据编号", isTrue: true },
                { title: "业务日期", isTrue: true },
                { title: "业务类型", isTrue: true },
                { title: "联系人", isTrue: true },
                { title: "联系电话", isTrue: true },
                { title: "联系地址", isTrue: true },
                { title: "物料名称", isTrue: true },
                { title: "数量", isTrue: true },
                { title: "单位", isTrue: true },
                { title: "仓库", isTrue: true },
                { title: "仓位", isTrue: true },
                { title: "操作人", isTrue: true },
                { title: "状态", isTrue: true },
            ],
            // 当前选中的多选框，代表当前展示的列
            checkedColumns: ["单据编号", "业务日期", "业务类型", "联系人", "联系电话", "联系地址", "物料名称", "数量", "单位", "仓库", "仓位", "操作人", "状态"],
            // 多选框的列表，列出表格的每一列
            checkBoxGroup: ["单据编号", "业务日期", "业务类型", "联系人", "联系电话", "联系地址", "物料名称", "数量", "单位", "仓库", "仓位", "操作人", "状态"]
        }
    },
    created() {
        this.getList()
    },
    watch: {
        checkedColumns(val) {
            let arr = this.checkBoxGroup.filter(i => !val.includes(i));
            this.colData.filter(i => {
                if (arr.indexOf(i.title) != -1) {
                    i.isTrue = false;
                } else {
                    i.isTrue = true;
                }
            });
            this.reload = Math.random()
        }
    },
    methods: {
        // 获取列表
        getList() {
            this.loading = true
            otherStorageList(this.addDateRange(this.queryParams)).then(res => {
                this.list = res.rows
                this.total = res.total
                this.loading = false
                this.getWarehouseList()
            })
        },
        // 获取仓库列表
        getWarehouseList(){
            queryWarehouse().then(res => {
                if(res.code == 200){
                    this.list.forEach(i => {
                        if(i.detailList){
                            i.detailList.forEach(y => {
                                let dataList = res.data.find(item => {
                                    return item.warehouseId == y.warehouse
                                })
                                if(dataList != undefined){
                                    y.warehouse = dataList.name
                                }
                            })
                        }
                    })
                }
            })
        },
        // 新建
        add(){
            this.$router.push({path:"/warehouseManagement/warehouse/otherStorage/add"})
        },
        // 搜索按钮
        handleQuery() {
            this.queryParams.pageNum = 1
            this.getList()
        },
        // 重置按钮
        resetQuery() {
            this.resetForm("queryForm")
            this.time = ''
            this.queryParams.startTime = ''
            this.queryParams.endTime = ''
            this.handleQuery()
        },
        // 查看详情
        handleView(row) {
            this.$router.push({path:"/warehouseManagement/warehouse/otherStorage/add", query:{id: row.id}})
        },
        // 时间选择器
        dataTime(){
            if(this.time){
                let day = 24 * 60 * 60 * 1000
                this.queryParams.startTime = this.parseTime(this.time[0])
                this.queryParams.endTime = this.parseTime(this.time[1].getTime() + day)
            }else if(this.time == null){
                this.queryParams.startTime = ''
                this.queryParams.endTime = ''
            }
        },
        // 多选框选中数据
        handleSelectionChange(row) {
            this.ids = row.map(item => item.id)
        },
        // 导出
        btnExport(){
            let a = this.colData.filter(i => {
                return i.isTrue == true
            })
            let b = []
            a.forEach((item) => {
                this.tableHeaderData.forEach((data) => {
                    if(item.title == data.label){
                        b.push({name: data.prop, value: item.title})
                    }
                })
            })
            const obj = b.reduce((result, item) => {
                result[item.name] = item.value
                return result
            }, {})
            if(this.ids.length > 0){
                let data = {
                    inOut: 1,
                    ids: this.ids,
                    headMap: obj,
                    updateBy: this.queryParams.updateBy,
                    startTime: this.queryParams.startTime ? this.queryParams.startTime : undefined,
                    endTime: this.queryParams.endTime ? this.queryParams.endTime : undefined
                }
                this.getExport(data)
            }else{
                let data = {
                    inOut: 1,
                    headMap: obj,
                    updateBy: this.queryParams.updateBy,
                    startTime: this.queryParams.startTime ? this.queryParams.startTime : undefined,
                    endTime: this.queryParams.endTime ? this.queryParams.endTime : undefined
                }
                this.getExport(data)
            }
        },
        getExport(data){
            this.$modal.loading("正在导出")
            exportOtherInboundOrder(data).then(res => {
                this.$modal.closeLoading()
                let blob = new Blob([res], {
                    type: res.type,
                })
                let downloadElement = document.createElement("a")
                let href = window.URL.createObjectURL(blob)
                downloadElement.href = href
                downloadElement.download = "其他入库单.xls";
                document.body.appendChild(downloadElement)
                downloadElement.click()
                window.URL.revokeObjectURL(href)
            })
        }
    }
}
</script>